﻿using System.Collections.Generic;
using System.Text;

namespace CommandTools
{
    /// <summary>
    /// 命令帮助文档
    /// </summary>
    public class CommandHelpDoc
    {
        /// <summary>
        /// 默认构造方法
        /// </summary>
        public CommandHelpDoc() { }

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="operation">命令名称</param>
        /// <param name="general">命令总体说明</param>
        /// <param name="syntaxDocPairs">命令可用的语法结构及其对应的说明</param>
        /// <param name="optionDocPairs">命令可用的选项</param>
        /// <param name="remarks">命令备注信息</param>
        public CommandHelpDoc(string operation, string general, Dictionary<string, string> syntaxDocPairs, Dictionary<string, string> optionDocPairs, string remarks)
        {
            Operation = operation;
            General = general;
            foreach (var item in syntaxDocPairs)
                SyntaxDocPairs.Add(item.Key, item.Value);
            foreach (var item in optionDocPairs)
                Options.Add(item.Key, item.Value);
            Remarks = remarks;
        }

        /// <summary>
        /// 命令名称
        /// </summary>
        public string Operation { get; private set; } = string.Empty;

        /// <summary>
        /// 命令总体功能介绍
        /// </summary>
        public string General { get; private set; } = string.Empty;

        /// <summary>
        /// 命令语法与说明
        /// </summary>
        public Dictionary<string, string> SyntaxDocPairs { get; } = new Dictionary<string, string>();

        /// <summary>
        /// 命令可用的选项与说明
        /// </summary>
        public Dictionary<string, string> Options { get; } = new Dictionary<string, string>();

        /// <summary>
        /// 命令的备注
        /// </summary>
        public string Remarks { get; private set; } = string.Empty;

        /// <summary>
        /// 获取命令语法中最长语法的长度
        /// </summary>
        /// <returns>最长语法的长度</returns>
        private int GetMaxSyntaxLength()
        {
            int temp = 0;
            foreach (var item in SyntaxDocPairs)
                temp = item.Key.Length > temp ? item.Key.Length : temp;
            return Operation.Length + 1 + temp;
        }

        /// <summary>
        /// 获取命令语法的长度
        /// </summary>
        /// <param name="syntax">要获取的命令语法</param>
        /// <returns>命令语法的长度</returns>
        private int GetSyntaxLength(string syntax)
        {
            return Operation.Length + 1 + syntax.Length;
        }

        /// <summary>
        /// 获取命令选项中最长选项的长度
        /// </summary>
        /// <returns>最长选项的长度</returns>
        private int GetMaxOptionLength()
        {
            int temp = 0;
            foreach (var item in Options)
                temp = item.Key.Length > temp ? item.Key.Length : temp;
            return 1 + temp;
        }

        /// <summary>
        /// 获取命令选项的长度
        /// </summary>
        /// <param name="option">命令选项</param>
        /// <returns>命令选项的长度</returns>
        private int GetOptionLength(string option)
        {
            return 1 + option.Length;
        }

        /// <summary>
        /// 格式化为适用于控制台显示的命令说明文档
        /// </summary>
        /// <returns>命令说明文档</returns>
        public string ToConsoleDoc()
        {
            int startPos = GetMaxSyntaxLength() + 2;
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append(Operation + ": ");
            stringBuilder.Append(General);
            stringBuilder.Append("\n\n");
            stringBuilder.Append("命令用法: ");
            foreach (var item in SyntaxDocPairs)
            {
                stringBuilder.Append("\n\t" + Operation);
                stringBuilder.Append(" " + item.Key);
                stringBuilder.Append(' ', startPos - GetSyntaxLength(item.Key));
                stringBuilder.Append(item.Value);
            }
            startPos = GetMaxOptionLength() + 2;
            stringBuilder.Append("\n\n可选参数:");
            foreach (var item in Options)
            {
                stringBuilder.Append(" \n\t-" + item.Key);
                stringBuilder.Append(' ', startPos - GetOptionLength(item.Key));
                stringBuilder.Append(item.Value);
            }
            stringBuilder.Append("\n\n备注信息: " + Remarks);
            return stringBuilder.ToString();
        }

        /// <summary>
        /// 输出为XMl格式的字符串
        /// </summary>
        /// <returns></returns>
        public string ToXmlDoc()
        {
            return string.Empty;
        }
    }
}
